package com.lindroy.androidplayer.viewmodels

import androidx.lifecycle.ViewModel
import com.lindroy.androidplayer.logic.network.repository.Repository
import com.lindroy.androidplayer.utils.KVUtil
import com.lindroy.androidplayer.utils.SingleLiveEvent
import com.lindroy.androidplayer.utils.mapSingle

/**
 * @author Lin
 * @date 2022/3/9
 * @function
 */
class SearchHotKeyViewModel : ViewModel() {

    private val _searchHistory = SingleLiveEvent<List<String>>()

    val hotKeysResp = Repository.searchHotkeyList

    val searchHistory = _searchHistory.mapSingle { it }

    val isShowHistory = searchHistory.mapSingle { it.isNotEmpty() }

    fun removeHistoryItem(keyword: String) {
        KVUtil.removeSearchHistory(keyword)
        getSearchHistory()
    }

    fun onClearAll() {
        KVUtil.setSearchHistory(listOf())
        _searchHistory.postValue(listOf())
    }

    fun getSearchHistory() {
        _searchHistory.postValue(KVUtil.searchHistory)
    }


}